package thread;
//分别使用串行和并行的方式进行 10亿的自增 执行两次
public class Demo104_Create {
    private static long count = 10000l;
    public static void main(String[] args) {
        //串行
        serial();
        //并行
        concurrency();

    }
    //串行
    public static void serial() {
        //记录开始的时间
        long begin = System.currentTimeMillis();
        long a = 0;
        for (int i = 0; i < count; i++) {
           a++;
        }
        long b = 0;
        for (int i = 0; i < count; i++) {
            b++;
        }
        //记录结束的时间
        long end = System.currentTimeMillis();
        System.out.println("串行执行时间：" + (end - begin));
    }
    //并行
    public static void concurrency() {
        //记录开始的时间
        long begin = System.currentTimeMillis();
        //创建线程01
        Thread thread01 = new Thread(() -> {
            long a = 0;
            for (int i = 0; i < count; i++) {
                a++;
            }
        });
        //启动线程01
        thread01.start();
        //创建线程02
        Thread thread02 = new Thread(() -> {
            long b = 0;
            for (int i = 0; i < count; i++) {
                b++;
            }
        });
        //启动线程02
        thread02.start();
        try {
            thread01.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        try {
            thread02.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //记录结束时间
        long end = System.currentTimeMillis();
        System.out.println("并行执行的时间：" + (end - begin));
    }
}
